package com.cdp.system.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.utils.SecurityUtils;
import com.cdp.common.utils.StringUtils;
import com.cdp.system.dto.SysStatisticsDTO;
import com.cdp.system.mapper.SysStatisticsMapper;
import com.cdp.system.service.ISysStatisticsService;
import com.cdp.common.core.domain.entity.SysStatistics;
import com.cdp.system.vo.SysStatisticsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SysStatisticsService 实现类
 */
@Service
public class SysStatisticsServiceImpl extends ServiceImpl<SysStatisticsMapper, SysStatistics> implements ISysStatisticsService {


    @Autowired
    private SysStatisticsMapper sysStatisticsMapper;

    /**
     * 查询统计表
     *
     * @param sysStatisticsDTO
     * @return
     */
    @Override
    public AjaxResult querySysStatistic(SysStatisticsDTO sysStatisticsDTO) {

        Long userId = SecurityUtils.getUserId();

        if(userId==null ||userId.equals("")){
            return AjaxResult.error(502,"获取用户id异常");
        }

        sysStatisticsDTO.setUserId(userId);
        List<SysStatisticsVO> sysStatisticsVOS = sysStatisticsMapper.querySysStatistic(sysStatisticsDTO);

        return AjaxResult.success(sysStatisticsVOS);
    }

    /**
     * 添加
     * @param statistics
     * @return
     */

    @Override
    public AjaxResult addStatistic(SysStatisticsDTO statistics) {
//        statistics.setCreateTime(DateTime.now());
//        statistics.setUserId(SecurityUtils.getUserId().intValue());  // 将 Long 转换为 Integer
        // 创建一个新的 SysStatistics 对象，去除 id 字段的设置
        SysStatistics sysStatistics = new SysStatistics();
        sysStatistics.setName(statistics.getName());
        sysStatistics.setRecordId(statistics.getRecordId());
        sysStatistics.setPdfUrl(statistics.getPdfUrl());
        sysStatistics.setCreateTime(DateTime.now());
        sysStatistics.setUserId(SecurityUtils.getUserId().intValue());
        // 执行插入操作，注意此时无需传递 id 字段
        sysStatisticsMapper.insert(sysStatistics);

        // 返回成功响应
        return AjaxResult.success();
    }




}
